import { login, logout, getInfo, getMenu, getUserShopinfo } from '@/api/user'
import { getToken, setToken, removeToken } from '@/utils/auth'
import { resetRouter } from '@/router'

const getDefaultState = () => {
  return {
    token: getToken(),
    name: '',
    avatar: '',
    menu: '',
    shopInfo: ''
  }
}

const state = getDefaultState()

const mutations = {
  RESET_STATE: (state) => {
    Object.assign(state, getDefaultState())
  },
  SET_TOKEN: (state, token) => {
    state.token = token
  },
  SET_NAME: (state, name) => {
    state.name = name
  },
  SET_AVATAR: (state, avatar) => {
    state.avatar = avatar
  },
  SET_MENU: (state, menu) => {
    state.menu = menu
  },
  SET_SHOPINFO: (state, shopInfo) => {
    state.shopInfo = shopInfo
  }
}

const actions = {
  // user login
  login({ commit }, userInfo) {
    console.log('login commit, userInfo', { commit }, userInfo)
    const { username, password } = userInfo
    return new Promise((resolve, reject) => {
      login({ username: username.trim(), password: password }).then(response => {
        commit('SET_TOKEN', response.code)
        setToken(response.code)
        resolve(response)
      }).catch(error => {
        console.log('error', error)
        reject(error)
      })
    })
  },

  // get user info
  getInfo({ commit, state }) {
    return new Promise((resolve, reject) => {
      getInfo(state.token).then(response => {
        const { data } = response

        if (!data) {
          reject('Verification failed, please Login again.')
        }

        const { name, avatar } = data

        commit('SET_NAME', name)
        commit('SET_AVATAR', avatar)
        resolve(data)
      }).catch(error => {
        reject(error)
      })
    })
  },

  // get user menu
  getMenu({ commit }) {
    return new Promise((resolve, reject) => {
      getMenu().then(response => {
        if (!response) {
          reject('Verification failed, please Login again.')
        }
        // const menus =
        // [{
        //   path: '/example',
        //   component: 'Layout',
        //   redirect: '/example/table',
        //   name: 'Example',
        //   meta: { title: 'Example', icon: 'example' },
        //   children: [
        //     {
        //       path: 'table',
        //       name: 'Table',
        //       component: 'commodity/index',
        //       meta: { title: 'Table', icon: 'table' }
        //     },
        //     {
        //       path: 'tree',
        //       name: 'Tree',
        //       component: 'tree',
        //       meta: { title: 'Tree', icon: 'tree' }
        //     }
        //   ]
        // }, {
        //   path: '/banner',
        //   component: 'Layout',
        //   children: [
        //     {
        //       path: 'banner',
        //       name: 'banner',
        //       component: 'banner',
        //       meta: { title: 'banner', icon: 'form' }
        //     }
        //   ]
        // }
        // ]
        const arr = []
        for (const i in response.detail.menu) {
          arr.push(response.detail.menu[i]) // 属性
          // arr.push(obj[i]); //值
        }
        commit('SET_MENU', arr)
        // commit('SET_MENU', menus)
        console.log(response.detail.menu, 111)
        resolve(response)
      }).catch(error => {
        reject(error)
      })
    })
  },

  // get user menu
  getUserShopinfo({ commit }) {
    return new Promise((resolve, reject) => {
      getUserShopinfo().then(response => {
        const { detail } = response

        // console.log(detail)

        if (!detail) {
          reject('Verification failed, please Login again.')
        }

        const { name, logo } = detail

        commit('SET_NAME', name)
        commit('SET_AVATAR', logo)
        console.log('SET_SHOPINFO detail', detail)
        commit('SET_SHOPINFO', detail)
        resolve(response)
      }).catch(error => {
        reject(error)
      })
    })
  },

  // user logout
  logout({ commit, state }) {
    return new Promise((resolve, reject) => {
      logout(state.token).then(() => {
        removeToken() // must remove  token  first
        resetRouter()
        commit('RESET_STATE')
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },

  // remove token
  resetToken({ commit }) {
    return new Promise(resolve => {
      removeToken() // must remove  token  first
      commit('RESET_STATE')
      resolve()
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}

